package com.swsc.solarverse.model;

import java.io.Serializable;

/**
 * @Author chencl
 * @Date 2021/04/28 22:39
 * @Version: 1.0
 * @Description 统一返回
 */
public class RestResp<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private static final int CODE_SUCCESS = 200;
    private static final int CODE_FAIL = 500;
    private static final String MSG_SUCCESS = "SUCCESS";
    private static final String MSG_FAIL = "FAIL";
    public final boolean success;
    public final int code;
    public final String message;
    public final T data;
    public final String path;

    public final Long currentTime;

    protected RestResp(boolean success, int code, String message, String path, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.path = path;
        this.data = data;
        this.currentTime = System.currentTimeMillis();
    }

    public static <T> RestResp<T> success(T data) {
        return new RestResp(true, CODE_SUCCESS, MSG_SUCCESS, null, data);
    }

    public static RestResp success() {
        return new RestResp(true, CODE_SUCCESS, MSG_SUCCESS, null, null);
    }

    public static <T> RestResp<T> success(String message, T data) {
        return new RestResp(true, CODE_SUCCESS, message, null, data);
    }

    public static RestResp fail() {
        return fail(CODE_FAIL, MSG_FAIL);
    }

    public static RestResp fail(String message) {
        return fail(CODE_FAIL, message);
    }

    public static RestResp fail(int code, String message) {
        return fail(code, message, null);
    }

    public static RestResp fail(int code, String message, String path) {
        return new RestResp(false, code, message, path, null);
    }

    public static <T> RestResp<T> OK(T data) {
        return success(data);
    }

    public static <T> RestResp<T> ok(T data) {
        return OK(data);
    }


    public static <T> RestResp<T> OK() {
        return OK(null);
    }

    public static <T> RestResp<T> ok() {
        return OK();
    }

    public static <T> RestResp<T> error(String message) {
        return fail(message);
    }

    public static void throwException(String message) {
        throw new RuntimeException(message);
    }
}